import asyncio
from concurrent.futures import ProcessPoolExecutor

from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt

# Create your views here.
from rest_framework.viewsets import ModelViewSet
from .models import Student
from .serializers import StudentModelSerializer
from rest_framework.settings import settings

# Create your views here.
class StudentViewSet(ModelViewSet):
    queryset = Student.objects.all()
    serializer_class = StudentModelSerializer


"""
默认情况下, 编写视图类时，如果继承的是django内置的django.view.View视图基类，
则视图方法中得到的request对象，
是django默认提供的django.core.handlers.wsgi.WSGIRequest
WSGIRequest这个请求处理对象，无法直接提供的关于json数据数据处理。
在编写api接口时很不方便，所以drf为了简写这块内容，在原来的HttpRequest的基础上面，新增了一个Request对象
这个Request对象是单独声明的和原来django的HttpRequest不是父子关系。
同时注意：
   要使用drf提供的Request请求处理对象，必须在编写视图类时继承drf提供的视图基类
   from rest_framework.views import APIView

   如果使用drf提供的视图基类APIView编写类视图，则必须使用来自drf提供的Request请求对象和Response响应对象
"""
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .utils import utils_add, fun, get


class ReqAPIView(APIView):
    def get(self, request):
        # await utils_add()
        pool = ProcessPoolExecutor(1)
        pool.submit(get)
        pool.shutdown(wait=False)

        # utils_add()
        print(request)  # <rest_framework.request.Request: GET '/req/req2?name=xiaoming&age=17&lve=swim&lve=code'>
        print(request.query_params)
        print(request.query_params.get("name"))  # xiaoming
        print(request.query_params.getlist("lve"))  # ['swim', 'code']
        return Response("okk")
    @csrf_exempt
    def post(self, request):
        # 获取请求体
        print(request.data.get('name'))  # {'name': 'xiaoming', 'age': 16, 'lve': ['swim', 'code']}
        print(request.FILES)  # 获取上传文件列表
        # 要获取django原生提供的HttpRequest对象，可以通过request._request来获取到
        print(request._request.META.get("Accept"))  # 当值为None时，drf默认在响应数据时按json格式返回
        # response = Response(data="not ok", status=204, headers={"Company":"Oldboy"})
        response = Response(data="not ok", status=status.HTTP_400_BAD_REQUEST, headers={"Company": "Oldboy"})
        return response